<?php
class ProjectFlagCategoriesController extends AppController
{
    public $name = 'ProjectFlagCategories';
    public function beforeFilter()
    {
        if (!Configure::read('Project.is_project_flag_enabled')) {
            throw new NotFoundException(__l('Invalid request'));
        }
        parent::beforeFilter();
    }
    public function admin_index()
    {
        $this->pageTitle = __l('Project Flag Categories');
        $this->_redirectGET2Named(array(
            'filter_id'
        ));
        $conditions = array();
        if (isset($this->request->params['named']['filter_id'])) {
            $this->request->data[$this->modelClass]['filter_id'] = $this->request->params['named']['filter_id'];
        }
        if (!empty($this->request->data[$this->modelClass]['filter_id'])) {
            if ($this->request->data[$this->modelClass]['filter_id'] == ConstMoreAction::Active) {
                $conditions[$this->modelClass . '.is_active'] = 1;
                $this->pageTitle.= __l(' - Approved');
            } else if ($this->request->data[$this->modelClass]['filter_id'] == ConstMoreAction::Inactive) {
                $conditions[$this->modelClass . '.is_active'] = 0;
                $this->pageTitle.= __l(' - Unapproved');
            }
            $this->request->params['named']['filter_id'] = $this->request->data[$this->modelClass]['filter_id'];
        }
        $this->ProjectFlagCategory->recursive = 1;
        $this->paginate = array(
            'conditions' => $conditions,
            'fields' => array(
                'ProjectFlagCategory.id',
                'ProjectFlagCategory.name',
                'ProjectFlagCategory.project_flag_count',
                'ProjectFlagCategory.is_active'
            ) ,
            'order' => array(
                'ProjectFlagCategory.id' => 'desc'
            )
        );
        $this->ProjectFlagCategory->recursive = 0;
        $this->set('projectFlagCategories', $this->paginate());
        $filters = $this->ProjectFlagCategory->isFilterOptions;
        $moreActions = $this->ProjectFlagCategory->moreActions;
        $this->set(compact('filters', 'moreActions'));
        $this->set('pending', $this->ProjectFlagCategory->find('count', array(
            'conditions' => array(
                'ProjectFlagCategory.is_active' => 0
            )
        )));
        $this->set('approved', $this->ProjectFlagCategory->find('count', array(
            'conditions' => array(
                'ProjectFlagCategory.is_active' => 1
            )
        )));
    }
    public function admin_add()
    {
        $this->pageTitle = __l('Add Project Flag Category');
        if (!empty($this->request->data)) {
            $this->ProjectFlagCategory->create();
            if ($this->ProjectFlagCategory->save($this->request->data)) {
                $this->Session->setFlash(__l('Project Flag Category has been added') , 'default', null, 'success');
                $this->redirect(array(
                    'action' => 'index'
                ));
            } else {
                $this->Session->setFlash(__l('Project Flag Category could not be added. Please, try again.') , 'default', null, 'error');
            }
        }
    }
    public function admin_edit($id = null)
    {
        $this->pageTitle = __l('Edit Project Flag Category');
        if (is_null($id)) {
            throw new NotFoundException(__l('Invalid request'));
        }
        if (!empty($this->request->data)) {
            if ($this->ProjectFlagCategory->save($this->request->data)) {
                $this->Session->setFlash(__l('Project Flag Category has been updated') , 'default', null, 'success');
                $this->redirect(array(
                    'action' => 'index'
                ));
            } else {
                $this->Session->setFlash(__l('Project Flag Category could not be updated. Please, try again.') , 'default', null, 'error');
            }
        } else {
            $this->request->data = $this->ProjectFlagCategory->read(null, $id);
            if (empty($this->request->data)) {
                throw new NotFoundException(__l('Invalid request'));
            }
        }
        $this->pageTitle.= ' - ' . $this->request->data['ProjectFlagCategory']['name'];
    }
    public function admin_delete($id = null)
    {
        if (is_null($id)) {
            throw new NotFoundException(__l('Invalid request'));
        }
        if ($this->ProjectFlagCategory->delete($id)) {
            $this->Session->setFlash(__l('Project Flag Category deleted') , 'default', null, 'success');
            $this->redirect(array(
                'action' => 'index'
            ));
        } else {
            throw new NotFoundException(__l('Invalid request'));
        }
    }
    public function admin_update()
    {
        if (!empty($this->request->data['ProjectFlagCategory'])) {
            $r = $this->request->data[$this->modelClass]['r'];
            $actionid = $this->request->data[$this->modelClass]['more_action_id'];
            unset($this->request->data[$this->modelClass]['r']);
            unset($this->request->data[$this->modelClass]['more_action_id']);
            $userIds = array();
            foreach($this->request->data['ProjectFlagCategory'] as $user_id => $is_checked) {
                if ($is_checked['id']) {
                    $userIds[] = $user_id;
                }
            }
            if ($actionid && !empty($userIds)) {
                if ($actionid == ConstMoreAction::Inactive) {
                    $this->ProjectFlagCategory->updateAll(array(
                        'ProjectFlagCategory.is_active' => 0
                    ) , array(
                        'ProjectFlagCategory.id' => $userIds
                    ));
                    $this->Session->setFlash(__l('Checked flag category has been inactivated') , 'default', null, 'success');
                } else if ($actionid == ConstMoreAction::Active) {
                    $this->ProjectFlagCategory->updateAll(array(
                        'ProjectFlagCategory.is_active' => 1
                    ) , array(
                        'ProjectFlagCategory.id' => $userIds
                    ));
                    $this->Session->setFlash(__l('Checked flag category has been activated') , 'default', null, 'success');
                } else if ($actionid == ConstMoreAction::Delete) {
                    $this->ProjectFlagCategory->deleteAll(array(
                        'ProjectFlagCategory.id' => $userIds
                    ));
                    $this->Session->setFlash(__l('Checked flag category has been deleted') , 'default', null, 'success');
                }
            }
        }
        $this->redirect(Router::url('/', true) . $r);
    }
}
?>